package com.orhon.smartcampus.modules.user.fetcher;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.dao.UsersDao;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;
import com.orhon.smartcampus.utils.R;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class UserFetcher implements DataFetcher {

    @Autowired
    UsersDao usersDao;

    @Autowired
    TokenService tokenService;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {

//        String token = tokenService.getSystemToken();
//        if(token==null) {
//            return  R.NoLogin();
//        }
//        Long schoolId = JwtUtils.getSchoolId(token);
        int id = Integer.parseInt(env.getArgument("id").toString());

        String hql = "from Users where deleted_at = null and id = "+id;
        Users item =  usersDao.findId(hql, null);
        if(item!=null) {


            GQLSchool gqlSchool = null;
            if (env.getSelectionSet().contains("school")){
                Schools schools = item.getSchool();
                gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
            }

            GQLUser gqlUser = UserMapper.MAPPER.mapUser(item);
            gqlUser.setSchool(gqlSchool);

            return gqlUser;
        }
        return null;
    }
}
